package com.ceres.customer.controller;

import com.ceres.customer.pojo.Customer;
import com.ceres.customer.service.CustService;
import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/customer")
public class CustController {

    @Autowired
    private CustService custService;

    /**
     * 农业组织添加方法
     * @param customer
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Customer customer) {
        try {
            //添加农场组织
            custService.add(customer);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }


    /**
     * 农业组织修改方法
     *
     * @param customer
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Customer customer) {
        try {
            custService.update(customer);
            return new Result(true, 2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 分页模糊查询
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(String keyword, int pageNum, int pageSize) {
        try {
            Page findPage = custService.findPage(keyword, pageNum, pageSize);
            return new Result(true, 2000, findPage);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }

    /**
     * 通过id查询农业组织
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id) {
        try {
            Customer customer = custService.findById(id);
            return new Result(true, 2000, customer);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, 2001);
        }
    }
}
